import pandas as pd

# 判断各单元格是否为空

df = pd.read_csv('property-data.csv')

print (df['NUM_BEDROOMS'])
print (df['NUM_BEDROOMS'].isnull())

# 指定空数据类型

missing_values = ["n/a", "na", "--"]
df = pd.read_csv('property-data.csv', na_values = missing_values)

print (df['NUM_BEDROOMS'])
print (df['NUM_BEDROOMS'].isnull())

# 删除空数据行

df = pd.read_csv('property-data.csv')

new_df = df.dropna()

print(new_df.to_string())

# 修改源数据DataFrame

df = pd.read_csv('property-data.csv')

df.dropna(inplace = True)

print(df.to_string())

# 移除 ST_NUM 列中字段值为空的行

df = pd.read_csv('property-data.csv')

df.dropna(subset=['ST_NUM'], inplace = True)

print(df.to_string())

# 替换空字段

df = pd.read_csv('property-data.csv')

df.fillna(12345, inplace = True)

print(df.to_string())

# 指定某一列替换空字段

df = pd.read_csv('property-data.csv')

df['PID'].fillna(12345, inplace = True)

print(df.to_string())

# 使用均值替换空字段

df = pd.read_csv('property-data.csv')

x = df["ST_NUM"].mean()

df["ST_NUM"].fillna(x, inplace = True)

print(df.to_string())

# 使用中位数替换空字段

df = pd.read_csv('property-data.csv')

x = df["ST_NUM"].median()

df["ST_NUM"].fillna(x, inplace = True)

print(df.to_string())

# 使用众数替换空字段

df = pd.read_csv('property-data.csv')

x = df["ST_NUM"].mode()

df["ST_NUM"].fillna(x, inplace = True)

print(df.to_string())
